define(["cookie"], function(cookie) {
    return {
        init: function() {
            let cookies = (cookie.get('cookies'));
            if (cookies) {
                //  console.log(cookies);
                /*        cookies = JSON.parse(cookies);
                       let idlist = [];
                       function current(currentValue) {
                           return currentValue.split(':')[0];
                       }
                       idlist = cookies.map(current); */ //   console.log(alist);
                let alist = cookies.split(',');


                function current(currentValue, index) {

                    return currentValue.split(':');
                }
                idlist = alist.map(current);
                let sid1 = [];
                let num1 = [];
                for (let i = 0; i < idlist.length; i++) {

                    sid1.push(idlist[i][0]);
                    num1.push(idlist[i][1]);

                }
<<<<<<< HEAD
                // console.log(idlist1);
=======
                // console.log(sid1);
>>>>>>> last commit


                for (let i = 0; i < sid1.length; i++) {
                    rendercart(sid1[i], num1[i]);
                }

                function rendercart(sid, num) {


                    $.ajax({
                        type: "GET",
                        url: `http://localhost/wmsj/php/getsid.php`,
                        data: {
                            sid: sid
                        },
                        dataType: "json"

                    }).done((data) => {
                        //    console.log(1);
                        //  console.log(data);
                        let strhtml = ``;
                        strhtml += `
                         
                         <tr data-pid="145_0">
                            <td class="state">
                                <span class="checkbox">
                                    <input type="checkbox" name="pids" value="145_0" checked="checked">
                                    <label class="curr" data-pid="145_0" ></label>
                                </span>
                            </td>
                            <td>



                                <dl class="cf">
                                    <dt class="f_left">
                                            <a href="details.html?sid=${data.sid}" target="_blank">
                                                
                                                <img src="${data.url}" alt="" title="${data.title}">
                                            </a>
                                        </dt>
                                    <dd class="f_left">
                                        <h1><a href="details.html?sid=${data.sid}" class="name ellipsis2" target="_blank" title="${data.title}">${data.title}</a></h1>

                                        <h2 class="sub_title ellipsis2" title="${data.title}">${data.title}</h2>



                                    </dd>
                                </dl>


                            </td>
                            <td class="discount">

                            </td>
                            <td>
                                <input id="price_145_0" type="hidden" value="6900">
                                <span class="amount number-input"><span class="reduce disable"></span>
                                
                                <input id="num_145_0" name="num_145_0" data-pid="145_0" type="text" value="${num}" min="1" data-stock="121" data-suit-number="1" maxlength="3" autocomplete="off">

                                <span class="plus"></span>
                                </span>

                                <p class="stock">库存
                                    <font>121</font>件</p>


                            </td>
                            <td class="price">¥
                                <font id="rmbPrice_145_0">${data.price}</font>
                            </td>
                            <td><a href="javascript:void(0)" class="del" data-pid="145_0"><span></span></a></td>
                        </tr>
                         `;




                        $('.cart_list').find('tbody').append(strhtml);


                        let price = 0;
                        let pricenum = 0;
                        let pricesis = 0;
                        let numheji = 0;
                        $('.curr').not('.allcheck').each(function() {

                            if ($(this).attr('data-pid') == '145_0') {
                                pricenum = $(this).parents('tr').find('#num_145_0').val();
                                price = $(this).parents('tr').find('#rmbPrice_145_0').text();


                                pricesis += +parseInt(pricenum) * parseInt(price);
                                numheji += +parseInt(pricenum);

                            } else {

                            }

                            let $xulie = $(this).parents('table').siblings('.total_bar').find('.f_right').find('p.f_right').find('span');

                            let $heji = $xulie.find('.ckp');
                            let $numji = $xulie.find('.ckn');
                            $heji.text(pricesis);
                            $numji.text(numheji);

                            console.log(pricesis);

                        });


                    })
                }



            }


            let $this = this;
            this.jisuan();

            this.check();
<<<<<<< HEAD
            /* ***********lizhi********* */
            $(document).ready(function() {
                if (cookie.get('users')) {
                    $(function() {
                        // console.log(1);
                        let usersname = cookie.get('users');
=======
            $this.calc();
            /* ***********lizhi********* */
            $(document).ready(function() {
                if (cookie.get('users')) {

                    let usersname = '';
                    $(function() {

                        usersname = cookie.get('users');
>>>>>>> last commit
                        // console.log(usersname);
                        $('.user-reg').css({ 'display': 'none' });
                        $('.usersdata').css({ 'display': 'block' });
                        $('.usersdata').text(usersname);
<<<<<<< HEAD
                        /*   console.log($('.checkusers').eq(1).find('.names').text(usersname)); */
                    });
                }
            });
            $this.calc();
=======

                    });

                    // let datas = cookie.get(cookies);
                    /*  $.ajax({
                         type: 'post',
                         url: `http://localhost/wmsj/php/userscartlist.php`,
                         data: {
                             usersname: usersname,
                             datas: datas
                         },
                         dataType: 'json'
                     }).done((data) => {
                         console.log(data);
                     }) */










                }
            });

>>>>>>> last commit


        },
        jisuan: function() {
            let $plus = $('.plus');
            let $reduce = $('.reduce');

            let $this = this;
            $('.wrap').on('click', '.plus', function() {

                let $val = $(this).siblings('#num_145_0').val();
                $val++;
                $(this).siblings('#num_145_0').val($val);

                // console.log($(this).siblings('#buyNumber').val());

                let price = 0;
                let pricenum = 0;
                let pricesis = 0;
                let numheji = 0;
                $('.curr').not('.allcheck').each(function() {
                    //console.log(1);
                    // console.log(this);
                    //  console.log($(this).attr('data-pid'));
                    if ($(this).attr('data-pid') == '145_0') {
                        // / console.log(2);
                        pricenum = $(this).parents('tr').find('#num_145_0').val();
                        price = $(this).parents('tr').find('#rmbPrice_145_0').text();
                        console.log(pricenum);
                        console.log(price);

                        pricesis += +parseInt(pricenum) * parseInt(price);
                        numheji += +parseInt(pricenum);

                    } else {

                    }

                    let $xulie = $(this).parents('table').siblings('.total_bar').find('.f_right').find('p.f_right').find('span');

                    let $heji = $xulie.find('.ckp');
                    let $numji = $xulie.find('.ckn');
                    $heji.text(pricesis);
                    $numji.text(numheji);

                    // console.log(pricesis);

                });






            });
            $('.wrap').on('click', '.reduce', function() {

                let $val = $(this).siblings('#num_145_0').val();
                $val--;
                $(this).siblings('#num_145_0').val($val);

                //console.log($(this).siblings('#buyNumber').val());

                let price = 0;
                let pricenum = 0;
                let pricesis = 0;
                let numheji = 0;
                $('.curr').not('.allcheck').each(function() {
                    //console.log(1);
                    // console.log(this);
                    //  console.log($(this).attr('data-pid'));
                    if ($(this).attr('data-pid') == '145_0') {
                        // / console.log(2);
                        pricenum = $(this).parents('tr').find('#num_145_0').val();
                        price = $(this).parents('tr').find('#rmbPrice_145_0').text();
                        console.log(pricenum);
                        console.log(price);

                        pricesis += +parseInt(pricenum) * parseInt(price);
                        numheji += +parseInt(pricenum);

                    } else {

                    }

                    let $xulie = $(this).parents('table').siblings('.total_bar').find('.f_right').find('p.f_right').find('span');

                    let $heji = $xulie.find('.ckp');
                    let $numji = $xulie.find('.ckn');
                    $heji.text(pricesis);
                    $numji.text(numheji);

                    // console.log(pricesis);

                });


            });

        },
        check: function() {
            let $cartform = $('#cartForm');
            //if ()
            let flag = 1;
            $('.all_checkbox').on('click', function() {
                if (!flag) {

                    $('label').addClass('curr');
                    $('label').siblings('input').prop('checked', 'checked');
                    flag = 1;
                    let price = 0;
                    let pricenum = 0;
                    let pricesis = 0;
                    let numheji = 0;
                    let flag1 = 0;
                    $('.curr').not('.allcheck').each(function() {

                        if ($(this).attr('data-pid') == '145_0') {
                            pricenum = $(this).parents('tr').find('#num_145_0').val();
                            price = $(this).parents('tr').find('#rmbPrice_145_0').text();


                            pricesis += +parseInt(pricenum) * parseInt(price);
                            numheji += +parseInt(pricenum);
                            flag1 = 1;
                        } else {
                            /*   pricesis = 0;
                              numheji = 0; */
                        }




                        let $xulie = $(this).parents('table').siblings('.total_bar').find('.f_right').find('p.f_right').find('span');
<<<<<<< HEAD

                        let $heji = $xulie.find('.ckp');
                        let $numji = $xulie.find('.ckn');
                        $heji.text(pricesis);
                        $numji.text(numheji);

                        // console.log(pricesis);

                    });
                } else {
                    $('label').removeClass('curr');
                    $('label').siblings('input').prop('checked', false);
                    flag = 0;


                    let $heji = $('#checkoutNum');
                    let $numji = $('#checkoutPrice');
                    let jjjjj = $('.ckn');
                    jjjjj.text(0);
                    $heji.text(0);
                    $numji.text(0);
                }




=======
>>>>>>> last commit

                        let $heji = $xulie.find('.ckp');
                        let $numji = $xulie.find('.ckn');
                        $heji.text(pricesis);
                        $numji.text(numheji);

                        // console.log(pricesis);

<<<<<<< HEAD
            });
=======
                    });
                } else {
                    $('label').removeClass('curr');
                    $('label').siblings('input').prop('checked', false);
                    flag = 0;
>>>>>>> last commit

            // let $labels = $('.checkbox').find('.checkbox').find('label');
            $('#cartForm').on('click', 'label', function() {
                //  console.log(1);
                // console.log($(this));
                let $input = $(this).siblings('input');
                // $input.prop('checked', $input.prop('checked'));
                //console.log($input.prop('checked'));
                let $inputcheck = $input.prop('checked');
                //console.log($inputcheck);
                if ($inputcheck) {
                    $input.prop('checked', false);
                    $input.siblings('label').removeClass('curr');
                } else {
                    $input.prop('checked', 'checked');
                    $input.siblings('label').addClass('curr');
                };
                // console.log(1);

<<<<<<< HEAD
=======
                    let $heji = $('#checkoutNum');
                    let $numji = $('#checkoutPrice');
                    let jjjjj = $('.ckn');
                    jjjjj.text(0);
                    $heji.text(0);
                    $numji.text(0);
                }







            });

            // let $labels = $('.checkbox').find('.checkbox').find('label');
            $('#cartForm').on('click', 'label', function() {
                //  console.log(1);
                // console.log($(this));
                let $input = $(this).siblings('input');
                // $input.prop('checked', $input.prop('checked'));
                //console.log($input.prop('checked'));
                let $inputcheck = $input.prop('checked');
                //console.log($inputcheck);
                if ($inputcheck) {
                    $input.prop('checked', false);
                    $input.siblings('label').removeClass('curr');
                } else {
                    $input.prop('checked', 'checked');
                    $input.siblings('label').addClass('curr');
                };
                // console.log(1);

>>>>>>> last commit

                // let $inputsiblings = [];



                let price = 0;
                let pricenum = 0;
                let pricesis = 0;
                let numheji = 0;
                $('.curr').not('.allcheck').each(function() {

                    if ($(this).attr('data-pid') == '145_0') {
                        pricenum = $(this).parents('tr').find('#num_145_0').val();
                        price = $(this).parents('tr').find('#rmbPrice_145_0').text();


                        pricesis += +parseInt(pricenum) * parseInt(price);
                        numheji += +parseInt(pricenum);

                    } else {

                    }

                    let $xulie = $(this).parents('table').siblings('.total_bar').find('.f_right').find('p.f_right').find('span');

                    let $heji = $xulie.find('.ckp');
                    let $numji = $xulie.find('.ckn');
                    $heji.text(pricesis);
                    $numji.text(numheji);

                    console.log(pricesis);

                });












            });







            /*  
                //input取不到
            $('#cartForm').on('click', 'input', function() {
                  //  console.log(1);
                  // console.log($(this));
                  let $input = $(this);
                  console.log($input);
                  // $input.prop('checked', $input.prop('checked'));
                  //console.log($input.prop('checked'));
                  let $inputcheck = $input.prop('checked');
                  //console.log($inputcheck);
                  if ($inputcheck) {
                      $input.prop('checked', false);
                      $(this).siblings('label').removeClass('curr');
                  } else {
                      $input.prop('checked', 'checked');
                      $(this).siblings('label').addClass('curr');
                  }




              }); */








        },
        calc: function() {

            let price = 0;
            let pricenum = 0;
            let pricesis = 0;
            let numheji = 0;
            $('.curr').not('.allcheck').each(function() {

                if ($(this).attr('data-pid') == '145_0') {
                    pricenum = $(this).parents('tr').find('#num_145_0').val();
                    price = $(this).parents('tr').find('#rmbPrice_145_0').text();


                    pricesis += +parseInt(pricenum) * parseInt(price);
                    numheji += +parseInt(pricenum);

                } else {

                }

                let $xulie = $(this).parents('table').siblings('.total_bar').find('.f_right').find('p.f_right').find('span');

                let $heji = $xulie.find('.ckp');
                let $numji = $xulie.find('.ckn');
                $heji.text(pricesis);
                $numji.text(numheji);

                console.log(pricesis);

            });
        }














    }

})